%% Legal blahblah
% Copyright 2008 Alex Rattner
% This file is part of Pursuit.
% 
% Pursuit is free software: you can redistribute it and/or modify
% it under the terms of the GNU General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
% 
% Foobar is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
% GNU General Public License for more details.
% 
% You should have received a copy of the GNU General Public License
% along with Pursuit.  If not, see <http://www.gnu.org/licenses/>.
%%

function main()
%% Preliminary setup
ServerIP = '127.0.0.1';
ServerPort = 1337;
name = 'Floating Eyeball'; role = 'observer';
GameName = 'GAME!!!';
GameType = 'pursuitv0.0';

%% Initial connection
sock = msconnect(ServerIP, ServerPort);
if (sock == -1) %bad connection
    disp('Bad Connection, buddy');
    return;
end

%% Give name and get game list
mssendraw(sock,uint8( ['{' name '}'] ));
games = getResponse(sock);

if strcmp(games, '{}') %list is empty, so make a new game
    gamestring = [ '{' GameName ',' GameType ',' role '}'];
    mssendraw(sock,uint8(gamestring));
    disp('Started a game');
else %join first game
    gamedata = split(games);
    gamestring = ['{' gamedata{1} ',' gamedata{2} ',' role '}'];
    mssendraw(sock, uint8(gamestring));
    disp('Joined a game');
end

figure(1); clf; hold on;

%% Run control code
while (1)
    %get raw state info from server
    dataString = getResponse(sock);

    %parse data into cell array
    dataCells = split(dataString);
    %check for game end conditions
    if strcmp(dataCells{1}, 'miss') || strcmp(dataCells{1}, 'hit')
        break;
    end
    %put parsed data into structs
    [A, B] = Strings2State( dataCells );

    %Plot this stuff
    quiver(A.x, A.y, A.u, A.v, A.color);
    quiver(B.x, B.y, B.u, B.v, B.color);
    pause(0.1);
    
    %build up response string
    actionstring = '{}';
    %send response string
    mssendraw(sock, uint8(actionstring));
end

%% End Game
msclose(sock);

%% Get String
function szResponse = getResponse(sock)
szResponse = [];

while ( isempty(szResponse) || (szResponse(length(szResponse)) ~= '}') )
    [val, res] = msrecvraw(sock,1); %get next char
    if (res < 0) %failure
        szResponse = '{}';
        break;
    end
    cNew = sprintf('%c', val);
    %append to string
    szResponse = [szResponse cNew];
end

%% Split String
function substrings = split(szIn)
substrings = {};
l = length(szIn);

if ( szIn(1) ~= '{' || szIn(l) ~= '}') %Bad Format
    return;
else
    szIn = szIn(2:(l-1)); %Chop off ends
    l = l-2;
end

commas = find(szIn == ',');
ind = 1;

%Split string into cell array of strings
while (l>0)
    if isempty(commas) %At last element
        substrings{ind} = szIn(1:length(szIn));
        szIn = '';
    else
        substrings{ind} = szIn(1:(commas(1)-1));
        szIn = szIn(commas(1)+1:length(szIn));
        if(length(commas) == 1)
            commas = [];
        else
            commas = commas(2:length(commas));
            commas = commas - length(substrings{ind}) - 1;
        end
    end
    ind = ind+1;
    l = length(szIn);
end

%% Strings to Statedata
function [A, B] = Strings2State (StateStrings)
%Initialize Structs
A = struct('name', '', 'role', '', 'x', 0, 'y', 0, 'theta', 0, 'u', 0,...
           'v', 0, 'omega', 0, 'color', 'k');
B = struct('name', '', 'role', '', 'x', 0, 'y', 0, 'theta', 0, 'u', 0,...
           'v', 0, 'omega', 0);

%Fill guy A
A.name = StateStrings(1);
A.role = StateStrings(2);
if strcmp(A.role, 'chaser')
    A.color = 'r';
elseif strcmp(A.role, 'chasee')
    A.color = 'g';
end
A.x = str2double(StateStrings{3});
A.y = str2double(StateStrings{4});
A.theta = str2double(StateStrings{5});
A.u = str2double(StateStrings{6});
A.v = str2double(StateStrings{7});
A.omega = str2double(StateStrings{8});

%Fill guy B
B.name = StateStrings(9);
B.role = StateStrings(10);
if strcmp(B.role, 'chaser')
    B.color = 'r';
elseif strcmp(B.role, 'chasee')
    B.color = 'g';
end
B.x = str2double(StateStrings{11});
B.y = str2double(StateStrings{12});
B.theta = str2double(StateStrings{13});
B.u = str2double(StateStrings{14});
B.v = str2double(StateStrings{15});
B.omega = str2double(StateStrings{16});